package com.wormholestack.tomcat.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @description： DisplayServlet
 * @Author MRyan
 * @Version 1.0
 */
public class DisplayServlet extends HttpServlet {

    /**
     * 处理 GET 方法请求的方法
     *
     * @param request  an {@link HttpServletRequest} object that
     *                 contains the request the client has made
     *                 of the servlet
     * @param response an {@link HttpServletResponse} object that
     *                 contains the response the servlet sends
     *                 to the client
     * @throws ServletException
     * @throws IOException
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contextPath = request.getContextPath() + request.getServletPath();
        String name = request.getParameter("name");
        // 设置响应内容
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String contextPathStr = String.format("Path :%s", contextPath);
        String nameStr = String.format("Name: %s", name);
        String docType =
            "<!DOCTYPE html> \n";
        out.println(docType +
            "<html>\n" +
            "<head><meta charset=\"utf-8\"><title>" + "Display" + "</title></head>\n" +
            "<body bgcolor=\"#2ed573\">\n" +
            "<h1 style=\"color: white;\">" + contextPathStr + "</h1>\n" +
            "<h1 style=\"color: white;\">" + nameStr + "</h1>\n"
        );
    }

    /**
     * 处理 POST 方法请求的方法
     *
     * @param request  an {@link HttpServletRequest} object that
     *                 contains the request the client has made
     *                 of the servlet
     * @param response an {@link HttpServletResponse} object that
     *                 contains the response the servlet sends
     *                 to the client
     * @throws ServletException
     * @throws IOException
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
